<template>
	<view class="container" :style="[bgStyle]">
		<view class="flow_y pt_16 plr_16 pbsafe_16">
			<view class="fc">
				<view class="ib_fx br_76 bo_1 boc_fff fz_16 fc_fff pr">
					<view class="pa l_0 b_0 subsection_block width_64 height_30 br_76 bc_fff transition zi_1"
						:style="[blockStyle]">

					</view>
					<view class="subsection_item width_64 lh_30 fc transition pr zi_2" :style="[txtStyle(0)]"
						@click="handleClickItem(0)">
						日榜
					</view>
					<view class="subsection_item width_64 lh_30 fc transition pr zi_2" :style="[txtStyle(1)]"
						@click="handleClickItem(1)">
						月榜
					</view>
					<view class="subsection_item width_64 lh_30 fc transition pr zi_2" :style="[txtStyle(2)]"
						@click="handleClickItem(2)">
						总榜
					</view>
				</view>
			</view>
			<view class="mt_82 bc_fff br_12 flow_h">
				<view class="tr fc_999">
					<view class="fc">
						排名
					</view>
					<view>
						用户
					</view>
					<view class="fc">
						红包金额
					</view>
				</view>
				<view class="tr" :style="index%2==1?'background: #F9F8F9;':''" v-for="item,index in dataList"
					:key="index">
					<view class="fc fb">
						<image class="ib width_36 height_36" :src="getRankNum(index)" mode="" v-if="index<3"></image>
						<text v-else>{{getRankNum(index)}}</text>
					</view>
					<view>
						{{$utils.hideName(item.userName||'')}} {{ type=='share'?'共享':'抢'}}红包获得
					</view>
					<view class="fc fc_primary">
						{{item.receiveAmount||0}}金豆
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import dataMixin from '@/mixins/data.js'
	import api from '@/service/config/api.js'
	export default {
		// mixins: [dataMixin],
		data() {
			return {
				tabItemLeft: [],
				activeIndex: 0,
				listUrl: '/marketing/applet/api/user/red_packet/recordRanking',
				type: 'custom',
				dataList: [],
				params: {
					type: 1,
					dealerId: this.$store.state.functionRange
				},
			};
		},
		computed: {
			blockStyle() {
				return {
					transform: `translateX(${this.tabItemLeft[this.activeIndex] + 'px'})`
				}
			},
			txtStyle() {
				return (index) => index == this.activeIndex ? {
					color: '#DE3F26'
				} : {}
			},
			getRankNum() {
				return (index) => {
					switch (true) {
						case index <= 2:
							return require(`../static/images/rank_${index+1}.png`)
						default:
							return index + 1
					}
				}
			},
			bgStyle() {
				if (this.type == 'custom') {
					return {
						backgroundImage: `url(${this.$ossUrl+'/redpacket/redpacket_bg5.png'})`,
						backgroundColor: '#FFCB80'
					}
				} else {
					return {
						backgroundImage: `url(${this.$ossUrl+'/redpacket/redpacket_bg4.png'})`,
						backgroundColor: '#FECDA2'
					}
				}
			}
		},
		onLoad(options) {
			this.type = options.type || 'custom'
			if (this.type == 'share') {
				this.listUrl = '/marketing/applet/api/user/red_packet/sharedRanking'
				uni.setNavigationBarTitle({
					title: '共享红包排名'
				})
			}
			this.getData(true)
			uni.createSelectorQuery().in(this).selectAll('.subsection_item')
				.boundingClientRect(
					data => {
						if (data.length) {
							this.tabItemLeft = data.map(item => {
								return item.left - data[0].left
							})
						}
					}).exec()
		},
		methods: {
			getData() {
				this.$utils.loading()
				api.get(this.listUrl, this.params).then(res=>{
					this.dataList = res.data
				})
			},
			handleClickItem(index) {
				this.activeIndex = index;
				this.$set(this.params, 'type', index + 1)
				this.getData(true)
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 0;
	}
</style>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		overflow: hidden;
		box-sizing: border-box;

		.tr {
			display: grid;
			align-items: center;
			grid-template-columns: 2fr 6fr 3fr;
			grid-column-gap: 20rpx;
			height: 88rpx;
			overflow: hidden;

			&>view {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}

		background-size: 100% auto;
		background-repeat: no-repeat;
	}
</style>